﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Bussiness;
using HTTT.DataAccess;
using HTTT.Utilities;
using HTTT.admin.controls;
using System.Configuration;

namespace HTTT.admin
{
    public partial class RequestDetail : System.Web.UI.Page
    {
        /// <summary>
        ///  View State ItemID
        /// </summary>
        private Guid? ItemID
        {
            get
            {
                object obj = this.ViewState["_ItemID"];
                if (obj == null)
                    return null; //default to show ID : 0 
                else
                    return (Guid)obj;
            }
            set
            {
                this.ViewState["_ItemID"] = value;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Guid TempID;
            if (Guid.TryParse(Request.QueryString["ID"], out TempID))
            {
                ItemID = TempID;
                ShowDetailRequest(TempID);
            }
            else
            {
                Response.Redirect("RequestList.aspx");
            }
            
        }

        private void ShowDetailRequest(Guid requestID)
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(requestID);

            switch (MyRequest.Action)
            {
                case "ADD_USER":
                    // Load add user request usercontrol
                    ucAddUserRequestDetail ucAddUser = (ucAddUserRequestDetail)LoadControl("controls/ucAddUserRequestDetail.ascx");
                    ucAddUser.ID = "ucAddUserRequestDetail";
                    ucAddUser.RequestID = requestID;
                    plDetail.Controls.Add(ucAddUser);
                    break;
                case "EDIT_USER":
                    // Load edit user request usercontrol
                    ucEditUserRequestDetail ucEditUser = (ucEditUserRequestDetail)LoadControl("controls/ucEditUserRequestDetail.ascx");
                    ucEditUser.ID = "ucAddUserRequestDetail";
                    ucEditUser.RequestID = requestID;
                    plDetail.Controls.Add(ucEditUser);
                    break;
                case "DELETE_USER":
                    // Load reset password request usercontrol
                    ucDeleteUserRequestDetail ucDelete = (ucDeleteUserRequestDetail)LoadControl("controls/ucDeleteUserRequestDetail.ascx");
                    ucDelete.ID = "ucResetRequestDetail";
                    ucDelete.RequestID = requestID;
                    plDetail.Controls.Add(ucDelete);
                    break;
                case "RESET_USER":
                    // Load reset password request usercontrol
                    ucResetRequestDetail ucReset = (ucResetRequestDetail)LoadControl("controls/ucResetRequestDetail.ascx");
                    ucReset.ID = "ucResetRequestDetail";
                    ucReset.RequestID = requestID;
                    plDetail.Controls.Add(ucReset);
                    break;
                case "BLOCK_USER":
                    // Load reset password request usercontrol
                    ucBlockUserRequestDetail ucBlock = (ucBlockUserRequestDetail)LoadControl("controls/ucBlockUserRequestDetail.ascx");
                    ucBlock.ID = "ucBlockUserRequestDetail";
                    ucBlock.RequestID = requestID;
                    plDetail.Controls.Add(ucBlock);
                    break;
                case "UN_BLOCK_USER":
                    // Load reset password request usercontrol
                    ucUnBlockUserRequestDetail ucUnBlock = (ucUnBlockUserRequestDetail)LoadControl("controls/ucUnBlockUserRequestDetail.ascx");
                    ucUnBlock.ID = "ucUnBlockUserRequestDetail";
                    ucUnBlock.RequestID = requestID;
                    plDetail.Controls.Add(ucUnBlock);
                    break;
                case "ADD_GROUP":
                    // Load reset password request usercontrol
                    ucAddGroupRequestDetail ucAddGroup = (ucAddGroupRequestDetail)LoadControl("controls/ucAddGroupRequestDetail.ascx");
                    ucAddGroup.ID = "ucAddGroupRequestDetail ";
                    ucAddGroup.RequestID = requestID;
                    plDetail.Controls.Add(ucAddGroup);
                    break;
                case "EDIT_GROUP":
                    // Load reset password request usercontrol
                    ucEditGroupRequestDetail ucEditGroup = (ucEditGroupRequestDetail)LoadControl("controls/ucEditGroupRequestDetail.ascx");
                    ucEditGroup.ID = "ucEditGroupRequestDetail ";
                    ucEditGroup.RequestID = requestID;
                    plDetail.Controls.Add(ucEditGroup);
                    break;
                case "DELETE_GROUP":
                    // Load reset password request usercontrol
                    ucDeleteGroupRequestDetail ucDeleteGroup = (ucDeleteGroupRequestDetail)LoadControl("controls/ucDeleteGroupRequestDetail.ascx");
                    ucDeleteGroup.ID = "ucDeleteGroupRequestDetail ";
                    ucDeleteGroup.RequestID = requestID;
                    plDetail.Controls.Add(ucDeleteGroup);
                    break;
                case "EDIT_GROUP_MEMBER":
                    // Load reset password request usercontrol
                    ucEditGroupMemberRequestDetail ucEditGroupMember = (ucEditGroupMemberRequestDetail)LoadControl("controls/ucEditGroupMemberRequestDetail.ascx");
                    ucEditGroupMember.ID = "ucEditGroupMemberRequestDetail ";
                    ucEditGroupMember.RequestID = requestID;
                    plDetail.Controls.Add(ucEditGroupMember);
                    break;
                case "ADD_OU":
                    // Load reset password request usercontrol
                    ucAddOURequestDetail ucAddOU = (ucAddOURequestDetail)LoadControl("controls/ucAddOURequestDetail.ascx");
                    ucAddOU.ID = "ucAddOURequestDetail";
                    ucAddOU.RequestID = requestID;
                    plDetail.Controls.Add(ucAddOU);
                    break;
                case "RENAME_OU":
                    // Load reset password request usercontrol
                    ucRenameOURequestDetail ucRenameOU = (ucRenameOURequestDetail)LoadControl("controls/ucRenameOURequestDetail.ascx");
                    ucRenameOU.ID = "ucRenameOURequestDetail";
                    ucRenameOU.RequestID = requestID;
                    plDetail.Controls.Add(ucRenameOU);
                    break;
                case "MOVE_OU":
                    // Load reset password request usercontrol
                    ucMoveOURequestDetail ucMoveOU = (ucMoveOURequestDetail)LoadControl("controls/ucMoveOURequestDetail.ascx");
                    ucMoveOU.ID = "ucMoveOURequestDetail";
                    ucMoveOU.RequestID = requestID;
                    plDetail.Controls.Add(ucMoveOU);
                    break;
                case "DELETE_OU":
                    // Load reset password request usercontrol
                    ucDeleteOURequestDetail ucDeleteOU = (ucDeleteOURequestDetail)LoadControl("controls/ucDeleteOURequestDetail.ascx");
                    ucDeleteOU.ID = "ucDeleteOURequestDetail";
                    ucDeleteOU.RequestID = requestID;
                    plDetail.Controls.Add(ucDeleteOU);
                    break;
                default:
                    break;

            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            // Do approve request
            HT_RequestBO RequestBO = new HT_RequestBO();
            HT_AuthenticateInfo AuthenInfo = new HT_AuthenticateInfo(
                                                ConfigurationManager.AppSettings["LDAPDomain"]
                                              , ConfigurationManager.AppSettings["LDAPPath"]
                                              , ConfigurationManager.AppSettings["LDAPUser"]
                                              , ConfigurationManager.AppSettings["LDAPPassword"]);
            ReturnValue<bool> ret = RequestBO.ApproveRequest((Guid)ItemID, AuthenInfo, Page.User.Identity.Name);
            //Response.Write(Utilities.Common.FormatAlertMsg(ret.Msg));
            if (ret.RetValue)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatMsgThenRedirect(ret.Msg, "RequestList.aspx"), true);
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Message", Common.FormatAlertMsg(ret.Msg));
            }
        }

        protected void btnDeny_Click(object sender, EventArgs e)
        {
            //Update this request is deny
            HT_RequestBO RequestBO = new HT_RequestBO();
            RequestBO.DenyRequest((Guid)ItemID, Page.User.Identity.Name);
            ClientScript.RegisterStartupScript(this.GetType(), "Redirect", Common.FormatMsgThenRedirect("Đã từ chối yêu cầu", "RequestList.aspx"), true);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("RequestList.aspx");
        }
    }
}